package com.travel.server.manager.impl.command.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.data.ExportOperateSaleTargetRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:12
 * @Desc:
 */
@Component
@Slf4j
public class ExportOperateSaleTargetCommand extends AbstractCommand<ExportOperateSaleTargetRequest, EmptyResponse> {

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(ExportOperateSaleTargetRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(ExportOperateSaleTargetRequest request) {
        checkInput(request);
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());

        Integer payAmount = orderDal.selectSumPayAmount(startTime, endTime);
        Integer payNum = orderDal.selectCountPayNum(startTime, endTime);

        List<String> title = Arrays.asList("总付款单数", "总付款金额");
        List<List<Object>> rows = new ArrayList<>();
        List<Object> objects = new ArrayList<>();
        objects.add(payNum);
        objects.add(CommonUtil.fenToYuan(payAmount).toString());
        rows.add(objects);
        String fileName = "销售量";

        ExcelData excelData = new ExcelData(title, rows, fileName);
        ExcelUtil.exportExcel(getHttpServletResponse(), fileName, excelData);

        return new EmptyResponse();
    }
}
